﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLDC.Common.Estate;
using System.Data;
using ZLDC.Common.BaseUser;


namespace ZLDC.Estate.TM
{
    public partial class UpdateTel : _PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string sql = " select tel,curtMobile,pwd from v_s_usr_lp where id=" + this.info.m_sEHRUserID + "";

                using (_Database db = new _Database("zlhq_user"))
                {
                    DataSet ds = db.Query(sql);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        tbTel.Text = ds.Tables[0].Rows[0]["tel"].ToString();//手机，电话
                        tbCurtMobile.Text = ds.Tables[0].Rows[0]["curtMobile"].ToString();//短号
                        Session["zlhq_pwd"] = ds.Tables[0].Rows[0]["pwd"].ToString();//密码
                    }
                }

            }
        }

        /// <summary>
        /// 修改用户密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //录入验证
            string sNew1 = PwdNew1.Value.ToString().Trim();
            string sNew2 = PwdNew2.Value.ToString().Trim();

            if (sNew1 == "")
            {
                MismatchLabel.Text = "新密码不能为空！";
                MismatchLabel.Visible = true;
                return;
            }
            if (sNew1 != sNew2)
            {
                MismatchLabel.Text = "两次输入的密码不一致！";
                MismatchLabel.Visible = true;
                return;
            }

            //校验旧密码
            string sOld = PwdOld.Value.ToString();

            string zlhqOld = DESEncryptor.Decript(Session["zlhq_pwd"].ToString());

            if (sOld != zlhqOld)
            {
                MismatchLabel.Text = "旧密码错误！";
                MismatchLabel.Visible = true;
                return;
            }
            int userid = _Convert.ToInt(_Utility.GetInfoBySession().m_sEHRUserID);//EHR中用户ID 
            string status = this.info.m_sStatus;

            //更新密码
            ZLDC.Topo.BLL.Estate.UserBLL.UpdateZlhqPwd(userid, sNew1, status);

            MismatchLabel.Text = "密码已修改，下次登陆请使用新密码！";
            MismatchLabel.Visible = true;
        }

        /// <summary>
        /// 修改用户电话
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (this.tbTel.Text.Trim() == string.Empty)
            {
                this.lblInfo.Text = "手机号不能为空!";
                return;
            }

            int userid = _Convert.ToInt(this.info.m_sEHRUserID.ToString());

            try
            {
                string sSql = string.Empty;

                string status = this.info.m_sStatus;

                switch (status.Trim())
                {
                    case "公盘"://部门
                        sSql = "update  t_zlhq_t_s_alldept set tel = '" + tbTel.Text.Trim() + "' where id = " + Convert.ToString(userid - 1000000);
                        break;
                    case "未入职"://跑盘员
                        sSql = "update  t_s_tmp_allusr set tel = '" + tbTel.Text.Trim() + "',curtMobile = '" + tbCurtMobile.Text.Trim() + "' where id = " + Convert.ToString(userid);
                        break;
                    default://默认更新用户
                        sSql = "update  t_s_allusr set mobile = '" + tbTel.Text.Trim() + "',curtMobile = '" + tbCurtMobile.Text.Trim() + "' where id = " + Convert.ToString(userid);
                        break;
                }


                using (_Database db = new _Database("zlhq_user"))
                {
                    db.ExecuteSql(sSql);
                }

            }
            catch (Exception ex)
            {
                this.lblInfo.Text = "修改失败：" + ex.Message;
            }
            this.lblInfo.Text = "修改成功!";

        }
    }
}
